using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Module.Common.Models;
using Module.Core.EntitiesInfo;

namespace Module.Core.DAO
{
	public class PaymentDAO
	{
		#region Fields

		protected string connectionStringName;

		#endregion		

		#region Methods

		/// <summary>
		/// Saves a record to the Payment table.
		/// </summary>
		public virtual int Insert(PaymentInfo paymentInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@order_id", paymentInfo.Order_id),
				new SqlParameter("@transaction_id", paymentInfo.Transaction_id),
				new SqlParameter("@created_on", paymentInfo.Created_on),
				new SqlParameter("@payment_type", paymentInfo.Payment_type),
				new SqlParameter("@transaction_status", paymentInfo.Transaction_status),
				new SqlParameter("@total_amount", paymentInfo.Total_amount),
				new SqlParameter("@net_amount", paymentInfo.Net_amount),
				new SqlParameter("@fee_amount", paymentInfo.Fee_amount),
				new SqlParameter("@merchant_id", paymentInfo.Merchant_id),
				new SqlParameter("@customer_name", paymentInfo.Customer_name),
				new SqlParameter("@customer_email", paymentInfo.Customer_email),
				new SqlParameter("@customer_phone", paymentInfo.Customer_phone),
				new SqlParameter("@customer_address", paymentInfo.Customer_address),
				new SqlParameter("@checksum", paymentInfo.Checksum),
                new SqlParameter("@IsActive", paymentInfo.IsActive),
                new SqlParameter("@CodeMember", paymentInfo.CodeMember)
                
			};

			return (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "Payment_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the Payment table.
		/// </summary>
		public virtual void Update(PaymentInfo paymentInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", paymentInfo.Id),
				new SqlParameter("@order_id", paymentInfo.Order_id),
				new SqlParameter("@transaction_id", paymentInfo.Transaction_id),
				new SqlParameter("@created_on", paymentInfo.Created_on),
				new SqlParameter("@payment_type", paymentInfo.Payment_type),
				new SqlParameter("@transaction_status", paymentInfo.Transaction_status),
				new SqlParameter("@total_amount", paymentInfo.Total_amount),
				new SqlParameter("@net_amount", paymentInfo.Net_amount),
				new SqlParameter("@fee_amount", paymentInfo.Fee_amount),
				new SqlParameter("@merchant_id", paymentInfo.Merchant_id),
				new SqlParameter("@customer_name", paymentInfo.Customer_name),
				new SqlParameter("@customer_email", paymentInfo.Customer_email),
				new SqlParameter("@customer_phone", paymentInfo.Customer_phone),
				new SqlParameter("@customer_address", paymentInfo.Customer_address),
				new SqlParameter("@checksum", paymentInfo.Checksum)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Payment_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the Payment table by its primary key.
		/// </summary>
		public virtual void Delete(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Payment_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the Payment table.
		/// </summary>
        public virtual PaymentInfo Select(string param, int type)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@param", param),
                new SqlParameter("@type", type)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Payment_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakePaymentInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

        public virtual DataTable DisplayTop(string codeMember, int topN,int type)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CodeMember", codeMember),
                new SqlParameter("@TopN", topN),
                new SqlParameter("@Type", type)
               
                               
			};
            using (DataTable dt = SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "Payment_SelectTop", parameters))
            {
                return dt;
            }
        }

		/// <summary>
		/// Selects all records from the Payment table.
		/// </summary>
		public CHRTList<PaymentInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Payment_SelectAll"))
			{
				CHRTList<PaymentInfo> paymentInfoList = new CHRTList<PaymentInfo>();
				while (dataReader.Read())
				{
					PaymentInfo paymentInfo = MakePaymentInfo(dataReader);
					paymentInfoList.Add(paymentInfo);
				}

				return paymentInfoList;
			}
		}

        public virtual int SelectCount(string customer_name, DateTime starDatetime, DateTime endDatetime)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{           
               new SqlParameter("@Customer_name", customer_name),
               new SqlParameter("@starDatetime", starDatetime),
               new SqlParameter("@endDatetime", endDatetime),
               new SqlParameter("@IsTotalRow", 1)
			};

            return (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "Payment_Search", parameters);
        }

        public virtual DataTable Search(string customer_name, DateTime starDatetime, DateTime endDatetime, int beginRow, int endRow, string orderByColumn)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{

                new SqlParameter("@Customer_name", customer_name),
                new SqlParameter("@starDatetime", starDatetime),
                new SqlParameter("@endDatetime", endDatetime),               
                new SqlParameter("@BeginRow", beginRow),
                new SqlParameter("@EndRow", endRow),
                new SqlParameter("@OrderByColumn", orderByColumn),
				new SqlParameter("@isTotalRow", 0)
			};

            return SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "Payment_Search", parameters);
        }


		/// <summary>
		/// Creates a new instance of the Payment class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual PaymentInfo MakePaymentInfo(SqlDataReader dataReader)
		{
			PaymentInfo paymentInfo = new PaymentInfo();
			paymentInfo.Id = SqlClientUtility.GetInt32(dataReader,DbConstants.PAYMENT.ID, 0);
			paymentInfo.Order_id = SqlClientUtility.GetString(dataReader,DbConstants.PAYMENT.ORDER_ID, String.Empty);
			paymentInfo.Transaction_id = SqlClientUtility.GetString(dataReader,DbConstants.PAYMENT.TRANSACTION_ID, String.Empty);
            paymentInfo.Created_on = SqlClientUtility.GetDateTime(dataReader, DbConstants.PAYMENT.CREATED_ON, DateTime.Now);
			paymentInfo.Payment_type = SqlClientUtility.GetInt32(dataReader,DbConstants.PAYMENT.PAYMENT_TYPE, 0);
			paymentInfo.Transaction_status = SqlClientUtility.GetInt32(dataReader,DbConstants.PAYMENT.TRANSACTION_STATUS, 0);
			paymentInfo.Total_amount = SqlClientUtility.GetFloat(dataReader,DbConstants.PAYMENT.TOTAL_AMOUNT, 0.0F);
			paymentInfo.Net_amount = SqlClientUtility.GetFloat(dataReader,DbConstants.PAYMENT.NET_AMOUNT, 0.0F);
			paymentInfo.Fee_amount = SqlClientUtility.GetFloat(dataReader,DbConstants.PAYMENT.FEE_AMOUNT, 0.0F);
			paymentInfo.Merchant_id = SqlClientUtility.GetInt32(dataReader,DbConstants.PAYMENT.MERCHANT_ID, 0);
			paymentInfo.Customer_name = SqlClientUtility.GetString(dataReader,DbConstants.PAYMENT.CUSTOMER_NAME, String.Empty);
			paymentInfo.Customer_email = SqlClientUtility.GetString(dataReader,DbConstants.PAYMENT.CUSTOMER_EMAIL, String.Empty);
			paymentInfo.Customer_phone = SqlClientUtility.GetString(dataReader,DbConstants.PAYMENT.CUSTOMER_PHONE, String.Empty);
			paymentInfo.Customer_address = SqlClientUtility.GetString(dataReader,DbConstants.PAYMENT.CUSTOMER_ADDRESS, String.Empty);
			paymentInfo.Checksum = SqlClientUtility.GetString(dataReader,DbConstants.PAYMENT.CHECKSUM, String.Empty);
            paymentInfo.IsActive = SqlClientUtility.GetBoolean(dataReader, DbConstants.PAYMENT.ISACTIVE, false);
            paymentInfo.CodeMember = SqlClientUtility.GetString(dataReader, DbConstants.PAYMENT.CODEMEMBER, String.Empty);
			return paymentInfo;
		}

		#endregion
	}
}
